package com.zcdyy.rentalofhouse.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.zcdyy.rentalofhouse.R;
import com.zcdyy.rentalofhouse.application.MyApplication;
import com.zcdyy.rentalofhouse.bean.Account;
import com.zcdyy.rentalofhouse.bean.AllRental;
import com.zcdyy.rentalofhouse.bean.OrderInfo;
import com.zcdyy.rentalofhouse.bean.SaleHouse;
import com.zcdyy.rentalofhouse.bean.ShopRent;
import com.zcdyy.rentalofhouse.bean.SingleRent;
import com.zcdyy.rentalofhouse.constant.Constants;
import com.zcdyy.rentalofhouse.listener.OnItemClickListener;
import com.zcdyy.rentalofhouse.ui.adapter.MyRentalAdapter;
import com.zcdyy.rentalofhouse.ui.adapter.RentalAdapter;
import com.zcdyy.rentalofhouse.ui.fragment.MainFragment;
import com.zcdyy.rentalofhouse.utils.BaseTranslucentActivity;
import com.zcdyy.rentalofhouse.utils.DividerItemDecoration;
import com.zcdyy.rentalofhouse.utils.RentalUtils;

import java.util.ArrayList;
import java.util.List;

public class MyRentalActivity extends BaseTranslucentActivity {
    private RecyclerView recyclerView;
    private TextView title;
    private MyRentalAdapter adapter;
    private List<OrderInfo> list = new ArrayList<>();
    private List<AllRental> list_allRental = new ArrayList<>();
    private List<SaleHouse> list_saleRent = new ArrayList<>();
    private List<SingleRent> list_singleRent = new ArrayList<>();
    private List<ShopRent> list_shopRent = new ArrayList<>();
    private Account loginInfo;
    public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what ==Constants.ResultCode.SUCCESS){
                adapter.notifyDataSetChanged();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        loginInfo = MyApplication.getInstance().readLoginUser();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_rental);
        initView();
        initData();
    }

    private void initData() {
        title.setText("我的发布");
        for (OrderInfo info: RentalUtils.list_order){
            if (info.getId().equals(loginInfo.getId())){
                list.add(info);
            }
        }
        for (AllRental a:RentalUtils.list_allRental){
            if (a.getId().equals(loginInfo.getId())){
                list_allRental.add(a);
            }
        }
        for (ShopRent s:RentalUtils.list_shopRent){
            if (s.getId().equals(loginInfo.getId())){
                list_shopRent.add(s);
            }
        }
        for (SingleRent a:RentalUtils.list_singleRent){
            if (a.getId().equals(loginInfo.getId())){
                list_singleRent.add(a);
            }
        }
        for (SaleHouse a:RentalUtils.list_saleHouse){
            if (a.getId().equals(loginInfo.getId())){
                list_saleRent.add(a);
            }
        }
        adapter = new MyRentalAdapter(this,list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        DividerItemDecoration decoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST);
        decoration.setDivider(R.drawable.divider_shape);
        recyclerView.addItemDecoration(decoration);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                switch (list.get(position).getType()){
                    case "0"://整租
                        Intent intent = new Intent(MyRentalActivity.this, AllRentalDetailActivity.class);
                        Bundle bundle = new Bundle();
                        for (AllRental a:list_allRental){
                            if (list.get(position).getTypeID().equals(a.getObjectId())){
                                bundle.putSerializable("allRental",a);
                                bundle.putString("title","整租");
                                intent.putExtra("bundle",bundle);
                                startActivity(intent);
                                break;
                            }
                        }
                        break;
                    case "1"://二手

                        Intent intent1 = new Intent(MyRentalActivity.this, SaleHouseDetailActivity.class);
                        Bundle bundle1 = new Bundle();
                        for (SaleHouse a:list_saleRent){
                            if (list.get(position).getTypeID().equals(a.getObjectId())){
                                bundle1.putSerializable("saleRent",a);
                                bundle1.putString("title","二手房");
                                intent1.putExtra("bundle",bundle1);
                                startActivity(intent1);
                                break;
                            }
                        }
                        break;
                    case "2"://单间
                        Intent intent2 = new Intent(MyRentalActivity.this, SingleDetailActivity.class);
                        Bundle bundle2 = new Bundle();
                        for (SingleRent a:list_singleRent){
                            if (list.get(position).getTypeID().equals(a.getObjectId())){
                                bundle2.putSerializable("singleRent",a);
                                bundle2.putString("title","单间");
                                intent2.putExtra("bundle",bundle2);
                                startActivity(intent2);
                                break;
                            }
                        }
                        break;
                    case "3"://商铺
                        Intent intent3 = new Intent(MyRentalActivity.this, AllRentalDetailActivity.class);
                        Bundle bundle3 = new Bundle();
                        for (ShopRent a:list_shopRent){
                            if (list.get(position).getTypeID().equals(a.getObjectId())){
                                bundle3.putSerializable("shopRent",a);
                                bundle3.putString("title","商铺");
                                intent3.putExtra("bundle",bundle3);
                                startActivity(intent3);
                                break;
                            }
                        }
                        break;
                }


            }
        });
    }

    private void initView() {
        title = (TextView) findViewById(R.id.top_tv_title);
        recyclerView = (RecyclerView) findViewById(R.id.recycler);
    }
}
